package com.cloudbroker.bcs.platform.ltb.middle.thread;

import com.cloudbroker.bcs.common.thread.ExecutorServiceManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by moyin18602 on 2016/9/26.
 */
public class FuncThread {

//    public static <V> List<V> submit(int count, Callable callable){
//        ExecutorService es = ExecutorServiceManager.getCachedThreadPool("HSBCPSSysFunc");
//        ArrayList<Future> futures = new ArrayList<Future>();
//        for (int i = 0; i < count; i++) {
//            futures.add(
//                es.submit(callable)
//            );
//        }
//        List result = new ArrayList();
//        for (int i = 0; i < count; i++) {
//            try {
//                result.add(futures.get(i).get());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (ExecutionException e) {
//                e.printStackTrace();
//            }
//        }
//        es.shutdown();
//        return result;
//    }

    public static <V> List<V> submit(List<Callable> list_callable){
        ExecutorService es = Executors.newCachedThreadPool();
        ArrayList<Future> futures = new ArrayList<Future>();
        System.out.println(Thread.currentThread().getName());
        for (Callable callable : list_callable) {
            futures.add(
                es.submit(callable)
            );
        }
        List result = new ArrayList();
        for (int i = 0; i < list_callable.size(); i++) {
            try {
                result.add(futures.get(i).get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        es.shutdown();
        return result;
    }
}
